data <- read.csv("IO&leader_FPA.csv")
library(xtsum)

## Main models (Table 2) ##
m1 <- glm(failure~IOs+election+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model 1
round(summary(m1)$coef, 3)
AIC(m1); BIC(m1); logLik(m1)

m2 <- glm(failure~IOs+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model 2
round(summary(m2)$coef, 3)
AIC(m2); BIC(m2); logLik(m2)

m3 <- glm(failure~IOsummit+election+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model 3
round(summary(m3)$coef, 3)
AIC(m3); BIC(m3); logLik(m3)

m4 <- glm(failure~IOsummit+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model 4
round(summary(m4)$coef, 3)
AIC(m4); BIC(m4); logLik(m4)

m5 <- glm(failure~IOsummit+I(IOs-IOsummit)+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model 5
round(summary(m5)$coef, 3)
AIC(m5); BIC(m5); logLik(m5)

m6 <- glm(failure~I(IOs-IOsummit)+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model 6
round(summary(m6)$coef, 3)
AIC(m6); BIC(m6); logLik(m6)

## Summary statistics (Table A2) ##
dd <- cbind(data["country"], data["year"], data["turnover"], data["failure"], data["IOs"], data["IOs_r"], data["IOsummit"], data["IOe"], data["IOp"], data["IOo"], data["lnGDPpc"], data["growth"], data["lnaid"], data["polity"], data["election"], data["threat"], data["lnoil"], data["age"], data["pcold"],  data["leave"], data["region"], data["io_region"], data["IOs_r.avg"])

dd <- dd[complete.cases(dd),]

round(apply(dd[,4:20], 2, mean, na.rm=T), 2)
round(apply(dd[,4:20], 2, median, na.rm=T), 2)
round(apply(dd[,4:20], 2, sd, na.rm=T), 2)
round(apply(dd[,4:20], 2, min, na.rm=T), 2)
round(apply(dd[,4:20], 2, max, na.rm=T), 2)

## Between and Within Variations (Table A3)) ##
xtsum(dd, "failure", id="country", t="year", na.rm = T)
xtsum(dd, "IOs", id="country", t="year", na.rm = T)
xtsum(dd, "IOsummit", id="country", t="year", na.rm = T)

## Robustness analysis (Table A4) ##
ma1 <- glm(failure~I(IOs>=52)+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model A1
round(summary(ma1)$coef, 3)
AIC(ma1); BIC(ma1); logLik(ma1)

ma2 <- glm(failure~I(IOsummit>=5)+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model A2
round(summary(ma2)$coef, 3)
AIC(ma2); BIC(ma2); logLik(ma2)

ma3 <- glm(failure~I(log(IOs))+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model A3
round(summary(ma3)$coef, 3)
AIC(ma3); BIC(ma3); logLik(ma3)

ma4 <- glm(failure~I(log(IOsummit+1))+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model A4
round(summary(ma4)$coef, 3)
AIC(ma4); BIC(ma4); logLik(ma4)

ma5 <- glm(failure~IOe+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model A5
round(summary(ma5)$coef, 3)
AIC(ma5); BIC(ma5); logLik(ma5)

ma6 <- glm(failure~IOp+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model A6
round(summary(ma6)$coef, 3)
AIC(ma6); BIC(ma6); logLik(ma6)

ma7 <- glm(failure~IOo+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model A7
round(summary(ma7)$coef, 3)
AIC(ma7); BIC(ma7); logLik(ma7)

ma8 <- glm(failure~IOe+IOp+IOo+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), data) #Model A8
round(summary(ma8)$coef, 3)
AIC(ma8); BIC(ma8); logLik(ma8)

## Two-stage instrumental variable analysis (Table A5) ##
ma9 <- lm(IOs~IOs_r.avg+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100), dd) #Model A9
round(summary(ma9)$coef, 3)
summary(ma9)

ma9_2 <- lm(IOs~lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100), dd)
anova(ma9, ma9_2) # F-test

dd$p <- predict(ma9)

ma10 <- glm(failure~p+lnGDPpc+growth+lnaid+polity+election+threat+lnoil+age+pcold+factor(country)+leave+I(leave^2/10)+I(leave^3/100),family=binomial(link="logit"), dd)  #Model A10
round(summary(ma10)$coef, 3)
AIC(ma10); BIC(ma10); logLik(ma10)


